//
//  TPUserBookingViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/26.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPUserBookingViewController.h"
#import "TPAppointmentViewController.h"
#import "TPUserCollectionViewController.h"
#import "TPProductDetailViewController.h"
#import "TPAppointDetailViewController.h"
@interface TPUserBookingViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
@property (nonatomic,strong)TPAppointmentViewController *appointVC;
@property (nonatomic,strong)TPAppointmentViewController *appointVC2;
@property (nonatomic,strong)UIPageViewController *pageViewController;

@property (nonatomic,weak)UIButton *resetButton;
@property (nonatomic,weak)UIButton *doneButton;
@end

@implementation TPUserBookingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"护理记录";
    [self sepTabbar];
    [self setup];
}
-(void)setup{
    
    
    
    _pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionSpineLocationKey:@0,UIPageViewControllerOptionInterPageSpacingKey:@2}];
    _pageViewController.delegate = self;
    _pageViewController.dataSource = self;
    _pageViewController.view.frame = CGRectMake(0, IOS7_TOP_Y + 44.0, IPHONE_WIDTH, IPHONE_HEIGHT - IOS7_TOP_Y - 44.0);
    _appointVC = [TPAppointmentViewController new];
    
    _appointVC.type = TPAppointmentTypeAppoint;
    _appointVC2 =[TPAppointmentViewController new];
    _appointVC2.type = TPAppointmentTypeBooking;
    [_pageViewController setViewControllers:@[_appointVC] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
    [self.view addSubview:_pageViewController.view];
    if(self.type == TPAppointmentTypeBooking)
        [_pageViewController setViewControllers:@[_appointVC2] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    self.doneButton.selected = self.type == TPAppointmentTypeBooking;
    self.resetButton.selected = !self.doneButton.selected;
    
    _appointVC.nav = _appointVC2.nav = self.navigationController;
    
}
////翻页控制器进行向前翻页动作 这个数据源方法返回的视图控制器为要显示视图的视图控制器
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(TPAppointmentViewController *)viewController{
    DLog(@"viewControllerBeforeViewController");
    
    if(viewController.type == TPAppointmentTypeAppoint)
    return nil;
    
   
    return _appointVC;
}
-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    TPAppointmentViewController *vc = (TPAppointmentViewController *)previousViewControllers.firstObject;
    self.doneButton.selected = vc.type == TPAppointmentTypeAppoint;
    self.resetButton.selected = !self.doneButton.selected;
    
      DLog(@"didFinishAnimating");
}
////翻页控制器进行向后翻页动作 这个数据源方法返回的视图控制器为要显示视图的视图控制器
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(TPAppointmentViewController *)viewController{
     DLog(@"viewControllerAfterViewController");
    
    if(viewController.type == 2)
        return nil;
   
    return _appointVC2;
}
//屏幕旋转触发的代理方法
- (UIPageViewControllerSpineLocation) pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation{
    return UIPageViewControllerSpineLocationMin;
}
////设置分页控制器的分页数
//- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
//    return 2;
//}
////设置初始的分页点
//- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
//    return 0;
//}




-(void )sepTabbar{
    
    
    UIView *tabbar = [[UIView alloc]init];
    tabbar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tabbar];
    [tabbar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0.0);
        make.height.mas_equalTo(44.0);
        make.top.mas_equalTo(IOS7_TOP_Y);
    }];
    
    
    UIButton *resetButton = [UIButton buttonWithFrame:CGRectZero title:@"护理记录" font:fontSystemOfSize(17) color:TPBlack_333333];
    resetButton.selected = YES;
    [resetButton setTitleColor:TPAllBackgroundColor forState:UIControlStateSelected];
    [tabbar addSubview:resetButton];
    _resetButton = resetButton;
    resetButton.tag = 111;
    [resetButton addTouchUpInsideTarget:self action:@selector(actionClick:)];
    
    UIButton *doneButton = [UIButton buttonWithFrame:CGRectZero title:@"我的预约" font:fontSystemOfSize(17) color:TPBlack_333333];
    [doneButton setTitleColor:TPAllBackgroundColor forState:UIControlStateSelected];
    doneButton.tag = 222;
    [tabbar addSubview:doneButton];
    [doneButton addTouchUpInsideTarget:self action:@selector(actionClick:)];
    
    [resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0.0);
        make.width.mas_equalTo(tabbar.mas_width).multipliedBy(0.5);
    }];
    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0.0);
        make.width.mas_equalTo(resetButton.mas_width);
    }];
    _doneButton = doneButton;
    [tabbar tp_addLineBottomWithColor:TPAllBackgroundColor];
    
    UIImageView *line = [[UIImageView alloc]init];
    line.backgroundColor = TP_COLOR_245;
    [tabbar addSubview:line];
    [tabbar bringSubviewToFront:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(tabbar.mas_centerX);
        make.height.mas_equalTo(tabbar.mas_height).multipliedBy(0.7);
        make.centerY.mas_equalTo(tabbar);
        make.width.mas_equalTo(1.5);
    }];
}
-(void)actionClick:(UIButton *)sender{
    
    sender.selected = YES;
    if(sender.tag == 111){
        [_pageViewController setViewControllers:@[_appointVC] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
        ((UIButton *)[sender.superview viewWithTag:222]).selected = NO;
    }else{
        ((UIButton *)[sender.superview viewWithTag:111]).selected = NO;
        [_pageViewController setViewControllers:@[_appointVC2] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    }
}

@end
